<?php

namespace App\Jobs;

use App\Models\User;
use App\Models\GoldRecord;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Redis;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Rainsens\Dinglog\Facades\Dinglog;

class ApplyGoldProductPayingJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected User $user;
    protected string $cacheId;

    public function __construct(User $user, $cacheId)
    {
        $this->user = $user;
        $this->cacheId = $cacheId;
    }

    public function handle()
    {
        $recordId = Redis::hGet($this->cacheId, 'gold_record_id');
        Dinglog::text($recordId);
        if ($recordId <= 0) return;
        
        if ($record = GoldRecord::find($recordId)) {
            cashier('goldcoin')->payStorm($record);
        }
    }
}
